package com.example.admin.goodnews.fragment.DianYingPaiHang;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.adapter.DailySalesAdapter;
import com.example.admin.goodnews.adapter.MonthlySalesAdapter;
import com.example.admin.goodnews.entity.DailySalesDataEntity;
import com.example.admin.goodnews.entity.MonthlySalesDataEntity;
import com.example.admin.goodnews.utils.GetCurrentTimeUtil;
import com.example.admin.goodnews.utils.HttpUtils;
import com.example.admin.goodnews.utils.NetWorkDataUrl;
import com.google.gson.Gson;

/**
 * 单月票房页面
 * Created by admin on 2016/10/13.
 */

public class MonthlySalesFragment extends Fragment {
    private RecyclerView recyclerview_tongyongbuju_rv;// 加载数据的列表
    private MonthlySalesDataEntity monthlySalesDataEntity;// 数据实体类
    private LinearLayoutManager linearLayoutManager;// 布局管理器
    private MonthlySalesAdapter monthlySalesAdapter;// 数据适配器
    private String time = GetCurrentTimeUtil.getCurrentTime();// 获取系统当前时间

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_tongyongbuju_rv, null);
        recyclerview_tongyongbuju_rv = (RecyclerView) view.findViewById(R.id.recyclerview_tongyongbuju_rv);
        getData();
        return view;
    }

    private void getData() {
        String param = NetWorkDataUrl.MONTHLYSALES_PARAM1 + time + NetWorkDataUrl.MONTYLYSALES_PARAM2;
        try {
            HttpUtils.doPostAsyn(NetWorkDataUrl.MONTHLYSALES_URL, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    monthlySalesDataEntity = gson.fromJson(result, MonthlySalesDataEntity.class);
                    if (monthlySalesDataEntity.getShowapi_res_body().getDatalist().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
                    recyclerview_tongyongbuju_rv.setLayoutManager(linearLayoutManager);
                    monthlySalesAdapter = new MonthlySalesAdapter(getActivity(), monthlySalesDataEntity);
                    recyclerview_tongyongbuju_rv.setAdapter(monthlySalesAdapter);
                    break;
            }
        }
    };

}
